package com.hsy.linemoney.modules.common;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.deparse.dpframe.tools.utils.DPUnitUtil;
import com.hsy.linemoney.R;

/**
 * Created by MartinKent on 2016/7/15.
 */
public class LoadingDialog extends Dialog {
    protected View rootView;
    protected ImageView topImage;
    protected ImageView shadow;
    private View contentView;

    private static final int[] images = {
            R.mipmap.loading_hand,
            R.mipmap.loading_papger_money,
            R.mipmap.loading_coin,
            R.mipmap.loading_purse
    };

    public LoadingDialog(Context context) {
        super(context, R.style.loading_dialog_style);
        init(context);
    }

    public LoadingDialog(Context context, int themeResId) {
        super(context, R.style.loading_dialog_style);
        init(context);
    }

    protected LoadingDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, R.style.loading_dialog_style);
        init(context);
    }

    private void init(Context context) {
        contentView = LayoutInflater.from(context).inflate(R.layout.loading_dialog, null);
        setContentView(contentView);
//        setCancelable(false);
//        setCanceledOnTouchOutside(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView(contentView);
    }

    int count = 0;
    ObjectAnimator animatorTop, animatorBottom;

    private void initView(View rootView) {
        topImage = (ImageView) rootView.findViewById(R.id.top_image);
        shadow = (ImageView) rootView.findViewById(R.id.shadow);

        animatorTop = ObjectAnimator.ofFloat(topImage, "TranslationY", 0, DPUnitUtil.dip2px(getContext(), 40), 0).setDuration(1000);
        animatorTop.setRepeatCount(-1);
        animatorTop.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationRepeat(Animator animation) {
                super.onAnimationRepeat(animation);
                count++;
                if (count == images.length) {
                    count = 0;
                }
                topImage.setImageResource(images[count]);
            }
        });
        animatorTop.start();

        PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 0.5f, 1f, 0.5f);
        PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 0.5f, 1f, 0.5f);
        animatorBottom = ObjectAnimator.ofPropertyValuesHolder(shadow, pvhY, pvhZ).setDuration(1000);
        animatorBottom.setRepeatCount(-1);
        animatorBottom.start();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        animatorTop.cancel();
        animatorBottom.cancel();
    }
}
